/***
 *ThreadModel.h - Thread Lock Model
 *    Copyright (c) 2005-2010, Beijing NetPosa Technologies Co.,Ltd. All rights reserved.
 *Purpose:
 **************************************************************************************/

#ifndef _ThreadModel_h_
#define _ThreadModel_h_

#if _MSC_VER > 1000
	#pragma once
#endif

#include "npsync.h"

////////////////////////////////////////////////////////////////////////////////
// Ported from Loki Library by Alvin Jiang

////////////////////////////////////////////////////////////////////////////////
// class NPSingleThreaded
// Implementation of the ThreadingModel policy used by various classes
// Implements a single-threaded model; no synchronization
////////////////////////////////////////////////////////////////////////////////

class NPSingleThreaded
{
public:
	struct AutoLock
	{
		AutoLock() {}
		explicit AutoLock(const NPSingleThreaded&) {}
	};
};

////////////////////////////////////////////////////////////////////////////////
// class NPObjectLevelLockable
// Implementation of the ThreadingModel policy used by various classes
// Implements a object-level locking scheme
////////////////////////////////////////////////////////////////////////////////

class NPObjectLevelLockable
{
public:
	NPObjectLevelLockable() { OSMutexInit(&mtx_); }
	~NPObjectLevelLockable() { OSMutexDestroy(&mtx_); }

    class AutoLock;
    friend class AutoLock;

	class AutoLock
	{
		NPObjectLevelLockable& host_;

		AutoLock(const AutoLock&);
		AutoLock& operator=(const AutoLock&);

	public:
		explicit AutoLock(NPObjectLevelLockable& host) : host_(host) { OSMutexLock(&host_.mtx_); }
		~AutoLock() { OSMutexUnlock(&host_.mtx_); }
	};

protected:
	OSMutex mtx_;
};

////////////////////////////////////////////////////////////////////////////////
// Change log:
// April 5, 2005: Added support for POSIX. Alvin Jiang
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
////////////////////////////////////////////////////////////////////////////////

#endif /* !_ThreadModel_h_ */
